package cn.itsource.crm.web.controller;

import cn.itsource.crm.domain.CustomerDevPlan;
import cn.itsource.crm.query.CustomerDevPlanQuery;
import cn.itsource.crm.service.ICustomerDevPlanService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.PageList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 潜在客户开发计划controller
 */
@Controller
@RequestMapping("/customerDevPlan")
@CrossOrigin//解决跨域问题
public class CustomerDevPlanController {

    @Autowired
    private ICustomerDevPlanService customerDevPlanService;

    /**
     * 查询所有数据信息
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)//批量查询用PATCH
    @ResponseBody
    public List<CustomerDevPlan> list(){
        System.out.println("patch请求，获取所有数据");
        System.out.println(customerDevPlanService.findAll());
        return customerDevPlanService.findAll();
    }

    /**
     * 分页查询
     * @param customerDevPlanQuery
     * @return
     */
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    public PageList<CustomerDevPlan> queryPage(@RequestBody CustomerDevPlanQuery customerDevPlanQuery){
        return customerDevPlanService.queryPage(customerDevPlanQuery);
    }


    /**
     * 根据id查询单个部门信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findOne/{id}",method = RequestMethod.GET)//查询单个用GET
    @ResponseBody
    public CustomerDevPlan findOne(@PathVariable Long id){
        System.out.println("通过id获取单个对象--  " + id);

        return customerDevPlanService.findOne(id);
    }

    /**
     * 保存部门信息
     * @param customerDevPlan
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.PUT)//保存用PUT
    @ResponseBody
    public AjaxResult save(@RequestBody CustomerDevPlan customerDevPlan){
        try {
            customerDevPlanService.save(customerDevPlan);
            System.out.println("保存成功"+customerDevPlan);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }

    /**
     * 更新部门信息
     * @param customerDevPlan
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)//POST用来修改信息
    @ResponseBody
    public AjaxResult update(@RequestBody CustomerDevPlan customerDevPlan){
        try {
            customerDevPlanService.update(customerDevPlan);
            System.out.println("更新成功");
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }


    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)//DELETE删除部门信息
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id){
        try {
            customerDevPlanService.delete(id);
            System.out.println("删除成功");
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }


    @RequestMapping(value = "/batchDelete",method = RequestMethod.PATCH)//DELETE删除部门信息
    @ResponseBody
    public AjaxResult batchDelete(@RequestBody Map<String,Object> params){
        System.out.println("进来了");
        try {
            // 获取传回来的id字符串
            String ids = params.get("ids").toString();
            // 通过逗号分割字符串，获得所有的id，在mapper中通过mybatis提供的动态循环遍历并删除数组中对应id的值就行
            String[] id = ids.split(",");
            // 根据自己的后台逻辑，调用service的方法，我就不写了
            customerDevPlanService.batchDelete(id);
            System.out.println("删除成功");
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }


}
